package com.newtouch.cq.lang.page;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;

import java.util.Date;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@SuppressWarnings("serial")
@Getter
@Setter
public class GenericPageRequest implements GenericPageAble {

    /**
     * 排序
     */
    @ApiModelProperty("排序")
    protected List<OrderItem> orders;
    @ApiModelProperty("开始时间")
    protected Date startTime;
    @ApiModelProperty("结束时间")
    protected Date endTime;
    /**
     * 页码从1开始，默认1
     */
    @ApiModelProperty("页码从1开始，默认1")
    protected Integer current;
    /**
     * 条数默认10
     */
    @ApiModelProperty("条数默认10")
    protected Integer size;

    @Override
    public int getCurrent() {
        return ObjectUtil.isNull(current) || current < 1 ? CURRENT_DEF : current;
    }

    @Override
    public int getSize() {
        return ObjectUtil.isNull(size) || size < 1 ? SIZE_DEF : size;
    }

    @Override
    public GenericPageRequest setCurrent(int current) {
        this.current = current;
        return this;
    }

    @Override
    public GenericPageRequest setSize(int size) {
        this.size = size;
        return this;
    }

    @Override
    public GenericPageAble defSort() {
        if (CollUtil.isEmpty(orders)) {
            orders = CollUtil.newArrayList(OrderItem.desc(StrUtil.toUnderlineCase(SORT_DEF)));
        }
        return this;
    }

    @Override
    public GenericPageRequest setPageRows(int page, int rows) {
        GenericPageAble.super.setPageRows(page, rows);
        return this;
    }

    public Page buildPage() {
         defSort();
        return new Page(getCurrent(),getSize()).addOrder(this.orders);
    }

}
